package com.gdut.wifidemo.model.impl;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;

import com.gdut.wifidemo.CustomApplication;
import com.gdut.wifidemo.common.RequestCallback;
import com.gdut.wifidemo.data.entity.Result;
import com.gdut.wifidemo.data.entity.Wifi;
import com.gdut.wifidemo.model.IWifiModel;
import com.gdut.wifidemo.strategy.StrategyContext;
import com.gdut.wifidemo.strategy.impl.KnnStrategy;
import com.gdut.wifidemo.utils.WifiUtils;

import java.util.ArrayList;
import java.util.List;

public class WifiModelImpl implements IWifiModel {

    private StrategyContext.Strategy strategy = StrategyContext.Strategy.KNN;

    @Override
    public void setStrategy(StrategyContext.Strategy strategy) {
        this.strategy = strategy;
    }

    @Override
    public void execute(final RequestCallback<Result> callback) {
        WifiUtils.getSearchWifiList(CustomApplication.getContext(), new RequestCallback<List<Wifi>>() {
            @Override
            public void onSuccess(List<Wifi> wifiList) {
                StrategyContext context;
                switch (strategy) {
                    case KNN:
                        context = new StrategyContext(new KnnStrategy());
                        break;
                    default:
                        context = new StrategyContext(new KnnStrategy());
                        break;

                }
                context.calculate(wifiList, callback);
            }

            @Override
            public void onFailure(final String message) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onFailure(message);
                        }
                    }
                });
            }

            @Override
            public void onLog(final String log) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onLog(log);
                        }
                    }
                });
            }
        });
    }
}
